package com.ponhu.ea2.goodsCenter.provider.task;

import com.ponhu.ea2.goodsCenter.provider.service.GoodsDataSyncService;
import com.ponhu.ea2.vo.GoodsDataAttributeSyncVo;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhaokai
 */
@Slf4j
public class GoodsDataSyncTask implements Runnable {

    @Resource
    GoodsDataSyncService goodsDataSyncService;

    private List<GoodsDataAttributeSyncVo> goodsDataAttributeSyncVos;

    public GoodsDataSyncTask(GoodsDataSyncService goodsDataSyncService, List<GoodsDataAttributeSyncVo> goodsDataAttributeSyncVos) {
        this.goodsDataSyncService = goodsDataSyncService;
        this.goodsDataAttributeSyncVos = goodsDataAttributeSyncVos;
    }

    @Override
    public void run() {
        process(goodsDataAttributeSyncVos);
    }

    /**
     * 遍历/保存数据
     *
     * @param goodsDataAttributeSyncVos
     */
    private void process(List<GoodsDataAttributeSyncVo> goodsDataAttributeSyncVos) {
        for (GoodsDataAttributeSyncVo dataAttributeSyncVo : goodsDataAttributeSyncVos) {
            // 通过库存编码查询是否已同步过信息
            boolean isExitInventoryData = goodsDataSyncService.checkIsExitInventoryData(dataAttributeSyncVo);
            if (isExitInventoryData) {
                goodsDataSyncService.updateGoodsDataAttributeData(dataAttributeSyncVo);
            } else {
                goodsDataSyncService.addGoodsDataAttributeData(dataAttributeSyncVo);
            }
        }
    }
}
